﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace SMSController
{
    public partial class frmSub : Form
    {
        //action is "1: MODIFY","2: Add new number","3: Add new shortcut"
        public static int  doaction;
        public static string shortcutTitle;
        private static string command;
        private static string parameter;
        private static string languageString;
        public frmSub(int action ,string title,string language)
        {
            doaction = action;
            shortcutTitle = title;
            languageString = language;
            InitializeComponent();

        }

        private void frmSub_Load(object sender, EventArgs e)
        {
            openFile.InitialDirectory = Environment.SpecialFolder.Programs.ToString();
            switch (doaction)
            {
                case 1:
                    Rules rules = new Rules(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml");
                    txtTitle.ReadOnly = true;
                    txtTitle.Text = shortcutTitle;
                    txtCommand.Text = rules.getShortcutCommand(shortcutTitle);
                    txtParameter.Text = rules.getShortcutParameter(shortcutTitle);
                    return;
                case 2:
                    foreach (Control ct in this.Controls)
                    {
                        try
                        {
                            ct.Visible = false;
                        }
                        catch
                        {
                        }

                    }
                    lbCommand.Visible=true;
                    lbCommand.Text = "Input one acceptable number:";
                    txtCommand.Visible = true;
                    txtCommand.Text = "";
                    return;
                case 3:
                    txtTitle.ReadOnly = false;
                    txtTitle.Text = "";
                    txtCommand.Text = "";
                    txtParameter.Text = "";
                    return;

            }
            LangPack lp = new LangPack(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml", languageString, this);
            this.Text = lp.getControlText(this.Name);
            foreach (Control ct in this.Controls)
            {
                try
                {
                    ct.Text = lp.getControlText(ct.Name);
                }
                catch
                {

                }
            }
        }

        private void mItemBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            DialogResult dr= openFile.ShowDialog();
            if (dr == DialogResult.OK)
            {
                command = openFile.FileName;
                txtCommand.Text  = command;
            }
        }

        private void mItemSubmit_Click(object sender, EventArgs e)
        {
            Rules rules = new Rules(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) + @"\config.xml");
            switch (doaction)
            {
                case 1:
                    command = txtCommand.Text.Trim();
                    parameter = txtParameter.Text.Trim();
                    rules.modifyShortcut(shortcutTitle, command, parameter);
                    this.Close();
                    return;
                case 2:
                    rules.addAcceptableNumber(txtCommand.Text);
                    this.Close();
                    return;
                case 3:
                    if (txtTitle.Text.Trim().Length == 0)
                    {
                        MessageBox.Show("Forget title?");
                        this.txtTitle.Focus();
                        this.Close();
                        return;
                    }
                    shortcutTitle = txtTitle.Text.Trim();
                    command = txtCommand.Text.Trim();
                    parameter = txtParameter.Text.Trim();
                    rules.addShortcut(shortcutTitle, command, parameter);
                    this.Close();
                    return;
                default:
                    this.Close();
                    return;
            }

        }




    }
}